import 'package:app/pages/contract_module/contract_model.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import 'contract_top_dialog_page.dart';

class ContractController extends GetxController{



  var page = 1;
  String replyType = '新建';
  String projectType = '';


  bool isTopShow = false;
  List<ContractItemModel>? dataArr = [];

  TextEditingController editingController = TextEditingController();

  EasyRefreshController easyRefreshController = EasyRefreshController();
  
  
  /// 数据请求
  requestData({var search,var state})async{

    var params = {
      'state':state,
      'page':page,
      'search':search
    };
    var json = await DioManager().kkRequest(Address.hrContractPage,params: params,method: 'get');

    ContractModel model = ContractModel.fromJson(json);

    if(page == 1){
      easyRefreshController.resetLoadState();
      dataArr!.clear();
    }
    if(model.data!=null){
      dataArr!.addAll(model.data!);
    }else if(model.data == null){
      BotToast.showText(text: '暂无更多');
      easyRefreshController.finishLoad(noMore: true);
    }


    // if (_model.data == null) {
    //   // dataArr.clear();
    //   print('暂无更多');
    //   easyRefreshController.finishLoad(noMore: true);
    //   // BotToast.showText(text: '暂无更多');
    //   update();
    //   // return;
    // }

    // if (page == 1) {
    //   easyRefreshController.resetLoadState();
    //   dataArr?.clear();
    //   dataArr?.addAll(_model.data!);
    // } else {
    //   if(_model.data!=null){
    //     dataArr?.addAll(_model.data!);
    //   }
    //   if (_model.data == null) {
    //     easyRefreshController.finishLoad(noMore: true);
    //     BotToast.showText(text: '暂无更多');
    //   }
    // }

    // dataArr?.addAll(_model.data!);

    update();
  }
  /// 下拉刷新
   pullToRefresh()async{
    page = 1;
    requestData(search: editingController.text,state:projectType );
  }
  /// 上拉加载
  onLoad()async{
    page++;
    requestData(search: editingController.text,state:projectType );
  }

  /// 点击的弹框
  topClick() async {
    isTopShow = true;

    print(replyType);
    var a;
    if (replyType == '新建') {
      a = 0;
    }
    if (replyType == '运行中') {
      a = 1;
    }
    if (replyType == '过期') {
      a = 2;
    }else if(replyType == '取消'){
      a = 4;
    }

    var data =
    await Get.dialog(ContractTopDialogPage(a), barrierColor: Colors.transparent);
    if (data != null) {
      print(data);
      isTopShow = data['isShow'];
      replyType = data['text'];
      projectType = data['reply_type'];
      page = 1;
      requestData(state: projectType);
    }
    update();
  }



  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    requestData();
  }

}